import {
	getAuditLogListApi
} from "@/api/auditLogManage";

const auditLogManage  = {
	namespaced: true,
	state: {
		auditLogList: [],
		paginationConfig:{
			current : 1,
			pageSize: 10,
			total: 50,
			pageSizeOptions: ['10', '30', '50', '100'],
			showQuickJumper: true,
			showSizeChanger: true,
			showTotal : (total)=>{
				let pageConfig = auditLogManage.state.paginationConfig
				return `共  ${total}  条记录   第  ${pageConfig.current} / ${Math.ceil(total/pageConfig.pageSize)}  页 `

			}
		},
		order: 'desc',
		orderBy: 'createdAt'
	},
	mutations: {
		SET_LOG_LIST: (state, list) => {
			state.auditLogList = list
		},
		SET_TOTAL: (state, total) => {
			state.paginationConfig.total = total
		},
		SET_CURRENT: (state, current) => {
			state.paginationConfig.current = current
		},
		SET_PAGE: (state, pages) => {
			state.paginationConfig.pageSize = pages
		},
		SET_ORDER: (state, {order, orderBy}) => {
			state.order = order
			state.orderBy = orderBy
		}
	},
	actions: {
		getAuditLogList ({ state, commit }, params) {
				const pageSize = state.paginationConfig.pageSize,
					page = state.paginationConfig.current,
					order = state.order,
					orderBy = state.orderBy
			params = Object.assign({ pageSize, page, order, orderBy }, params)
			return new Promise((resolve, reject) => {
				let msg = null
				getAuditLogListApi(params)
					.then(res => {
						if (res.code === 0) {
							msg = res
							const { data } = res
							commit('SET_LOG_LIST', data.data)
							commit('SET_TOTAL', data.total)
							commit('SET_CURRENT', data.current)
							commit('SET_PAGE', data.size)
							resolve(res)
						} else {
							reject(res)
						}
					}).catch(error => {
					console.error(error)
					reject(error)
				})
			})
		},
	},
	getters: {

	}
}

export default auditLogManage
